var MOD_PAG= {};

// Global iterator, shared by page_item iterator
MOD_PAG.PAGE_ITERATOR="";
MOD_PAG.LASTPAGE=[];
MOD_PAG.LASTPAGEITEM=[];

// page iterator:
// A page contains at most GetMaxClaimsPerPage claims
// the value of the iterator is the index of the page
// this iterator is to be used with page_item iterator 

function page_iterator(section) {
  // cache by sections (?)
  if(!section) {
    this.section="default";
  }
  this._tab=filter_toTab();
  this._last=this._tab.length-1;

  //alert(this._tab[this._last]);
  //  alert("total "+this._tab.length+"->first "+this._tab[0]+" last "+this._tab[this._last]);
 
  this._pagesize=parseInt(GetMaxClaimsPerPage());  
  this._lastpage=Math.ceil((this._last+1)/this._pagesize);
  
  //alert(this._last+"/"+this._pagesize+" -> "+(this._last/this._pagesize)+" ->"+this._lastpage);
 
  if(MOD_PAG.LASTPAGE[this.section]) { // remember last page
    //alert("reuse "+section);
    this._page=MOD_PAG.LASTPAGE[this.section];
  } else {
    //alert("restartfrom 1");
    this._page =1; 
  }

  this._changed(this._page);
}  

page_iterator.prototype._changed=function(page) {
  MOD_PAG.LASTPAGE[this.section]=page;
  //alert("page is "+page);
  this._firstitem=((page-1)*this._pagesize);
  this._lastitem=Math.min(this._firstitem+this._pagesize-1,this._last);  
  this._itemset=[];
  for(var idx=this._firstitem ; idx <= this._lastitem; idx++) {
    this._itemset[this._tab[idx]]=1;
  }
}

page_iterator.prototype.prev    = function() {  this._page--; this._changed(this._page)}
page_iterator.prototype.next    = function() {  this._page++; this._changed(this._page)}
page_iterator.prototype.getValue=function() {  return this._page;}
page_iterator.prototype.end = function() {  return this._page> this._lastpage;}
page_iterator.prototype.last = function() {  return this._lastpage;}
page_iterator.prototype.isLast = function() {  return this._page== this._lastpage;}
page_iterator.prototype.isInPage=function(cl) {  return this._itemset[cl];}
page_iterator.prototype.setValue= function(n) {  
  if(n>=1 && n <= this._lastpage) {
    this._page=n;
    this._changed(this._page);
  }  
}

// pageitem_iterator:
// iterates through the items contained on the current page of a page iterator;
 
function pageitem_iterator(page_iter,section) {
  this.base=array_iterator;
  //alert("P "+page_iter._firstitem+" "+page_iter._lastitem);
  this.base(page_iter._tab,page_iter._firstitem,page_iter._lastitem);
  //alert("page item "+this.first()+" -"+this.last());
}
pageitem_iterator.prototype=new array_iterator;
pageitem_iterator.prototype.constructor=pageitem_iterator;
pageitem_iterator.prototype.getfocus = function() { 
  return MOD_PAG.LASTPAGEITEM[section];
}
pageitem_iterator.prototype.setfocus = function (cl) {
  MOD_PAG.LASTPAGEITEM[section]=cl;
}


function InitPageClaimIterator(opt,section) {
  if(opt=="flush") {
    MOD_PAG.LASTPAGE=[];
    MOD_PAG.PAGE_ITERATOR=new page_iterator(section);
  } else  if(opt!="init" || !MOD_PAG.PAGE_ITERATOR)  { 
    MOD_PAG.LASTPAGE[section|| "default"]="";
    MOD_PAG.PAGE_ITERATOR=new page_iterator(section);
  }
}
         
function PageClaim_Iterator(section) {
  if(!MOD_PAG.PAGE_ITERATOR) {
    alert("Bug: Global page iterator not initalized");
    quit(0);
  }  
  return new pageitem_iterator(MOD_PAG.PAGE_ITERATOR,section);
}


function prev_page(callback) {
  MOD_PAG.PAGE_ITERATOR.prev();
  eval(callback);
}

function next_page(callback) {
  MOD_PAG.PAGE_ITERATOR.next();
  eval(callback);
}

function goto_page(page,callback) {
  MOD_PAG.PAGE_ITERATOR.setValue(page);
  eval(callback);
}

function html_pages(callback) {
  var html="";
  if(GetMaxClaimsPerPage()< MOD_FTR._CLSSHOWLENGTH) {
    html+="<fieldset><legend>Page</legend>";
    if(MOD_PAG.PAGE_ITERATOR.getValue()>1) {
      html+="<input TYPE=Button onclick='goto_page(1,\""+callback+"\");' value='|<' />";
      html+="<input TYPE=Button onclick='prev_page(\""+callback+"\");' ACCESSKEY='P' value='<' />";
    }
    html+="<input type=text size=2 value='"+MOD_PAG.PAGE_ITERATOR.getValue()+"' onChange='goto_page(this.value,\""+callback+"\");'>";
    html+="/<input type=text size=2 value='"+MOD_PAG.PAGE_ITERATOR.last()+"' readonly>";
    if(!MOD_PAG.PAGE_ITERATOR.isLast()) {
      html+="<input TYPE=Button onclick='next_page(\""+callback+"\");' ACCESSKEY='N' value='>' />";
      html+="<input TYPE=Button onclick='goto_page("+MOD_PAG.PAGE_ITERATOR.last()+",\""+callback+"\");' value='>|' />";
    }
    html+="</fieldset>";
  }
  return html;
}

function IsClaimInPage(cl) {
  return MOD_PAG.PAGE_ITERATOR.isInPage(cl);//_CLSSHOWPAGE[cl];
}

